<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController
{
	const STATUS_DRAFT = 1;
	const STATUS_OK = 2;
	const STATUS_DELETED = 3;
	/**
	 * @var string the default layout for the controller view. Defaults to 'column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout='column1';
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu=array();
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();

	public function filters()
    {
        return array(
            'accessControl',
        );
    }
    /*
    public function accessRules()
	{
		return array(
			array('allow', // allow authenticated users to access all actions
				'users'=>array('@'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
	*/

	public function responseJSON($errorCode,$data=""){
		header("Content-type: application/json");
		header("Access-Control-Allow-Origin: *");
		if(is_array($data)){
			$res = json_encode(array("response"=>array("code"=>0,"message"=>"success"),"data"=>$data));  	
		}else{
			$res = json_encode(array("response"=>array("code"=>500,"message"=>$data)));
		}
		print $res;
		Yii::app()->end();  
	}

	public function createList($tablename, $criteria){
		if ( isset( $_GET['pagecur'] ) )
			$pagecur = -1 + $_GET['pagecur'];
		else
			$pagecur = 0;

		if ( isset( $_GET['pageper'] ) )
			$pageper = $_GET['pageper'];
		else
			$pageper = Yii::app()->params['pagenum'];

		$dataProvider = new CActiveDataProvider( $tablename, array(
				'pagination' => array(
					'pageSize' => $pageper,
					'currentPage' => $pagecur,
				),
				'criteria'=>$criteria,
			) );
		$list = array();
		
		foreach ( $dataProvider->getData() as $key => $value ) {
			$list[] = $value->attributes;
		}

		$pagination = $dataProvider->getPagination();
		$page = array(
			"pageper" => 0+$pagination->pageSize,
			"pagesum" => 0+$pagination->pageCount,
			"pagecur" => 1+$pagination->currentPage,
			"sum" => 0+$pagination->itemCount,
		);

		//$this->responseJSON(0, array( "list" => $list, "page" => $page ) );
		return array( "list" => $list, "page" => $page );
	}

	public function log($content)
	{
		$model = new Log;
		$model->attributes = array(
			'class'=>$this->getId(),
			'action'=>$this->getAction()->getId(),
			'content'=>$content,
			);
		$model->save();
	}

	public function message($from, $to, $content)
	{
		$model = new message;
		$model->attributes = array(
			'from' => $from,
			'to' => $to,
			'content' => $content,
			'type' => 1,
			);
		$model->save();
	}

	public function export_csv($filename) { 
	    header("Content-type:text/csv"); 
	    header("Content-Disposition:attachment;filename=".$filename); 
	    header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
	    header('Expires:0'); 
	    header('Pragma:public'); 
	}

	public function checkAuth() {
		$user = User::model()->findByPk(Yii::app()->user->id);
		$user_type = $user->getAttribute("type");
		return true;
	}
}